/*****************************************************************
/     filename:   hw8.y
/
/  description:   Implements a parser for logical expressions
/
/       author:   Szechy, Jared
/     login id:   cps444-n1.10
/
/        class:   CPS 444
/   instructor:   Perugini
/   assignment:   Homework #8
/
/     assigned:   December 2, 2009
/          due:   December 14, 2009
/
/***************************************************************/
%token BOOL
%left '~' '&' '|'
%{
#include<string.h>
#include<stdio.h>
#define YYDEBUG 0
#define SIZE 255
char* temp;
%}

%%

program: program s '\n'	{ printf ("%s is %s\n", temp, ($2==0)?"false":"true");
                           *temp = 0; }
         | error '\n'   { printf ("invalid sentence.\n");
                           *temp = '\0';
                           yyclearin; /* discard lookahead */
                           yyerrok; }
         |
         ;

s:       '(' s ')'      { $$ = $2; }
         | s '&' s      { $$ = $1 & $3; }
         | s '|' s      { $$ = $1 | $3; }
         | '~' s        { $$ = ($2==0)?-1:0; }
         | BOOL         { $$ = $1; }
         ;

%%

int yyerror (char* s) {
   fprintf (stderr, "%s\n", s);
   return 0;
}

int main (void) {
   temp = (char*) malloc (sizeof(char)*SIZE);
#if YYDEBUG
   yydebug = 1;
//   yy_flex_debug = 1;
#endif
   yyparse();
   return 0;
}
